from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from chat import model
import pickle
import chromadb

db_kg_path="./storege"
LM=model(db_kg_path)

app = FastAPI()

# 允许跨域请求（方便前端开发）
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 存储接收到的消息
messages = []

@app.post("/send-message")
async def receive_message(message: dict):
    """接收前端发送的消息"""
    messages.append(message["text"])
    return {"status": "success", "message": "Message received"}

@app.get("/get-messages")
async def send_messages():
    """返回所有接收到的消息"""
    response=[]
    response.append(LM.query(messages[-1]))
    print(response)
    return {"messages": response}